# 推导式

print('---------------列表推导式------------------')
# 作⽤：⽤⼀个表达式创建⼀个有规律的列表或控制⼀个有规律列表。
# 列表推导式⼜叫列表⽣成式
# 创建⼀个0-10的列表
list1 = []
for i in range(11):
    list1.append(i)
    pass
print(list1)
list1 = []
list1 = [i for i in range(11)]
print(list1)
print()
print('--------------带if的列表推导式-------------------')
# 创建0-10的偶数列表
list1 = [i for i in range(0, 11, 2)]
print(list1)
list1 = []
list1 = [i for i in range(11) if i % 2 == 0]
print(list1)
print('----------------多个for循环实现列表推导式-----------------')
# [(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
list1 = []
list1 = [(i, j) for i in range(1, 5) if i % 2 == 0 for j in range(3)]
print(list1)
print('---------------字典推导式------------------')
list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'man']
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)
# 创建⼀个字典：字典key是1-5数字， value是这个数字的2次⽅。
dict1 = {i: i ** 2 for i in range(1, 5)}
print(dict1)
counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99}
# 需求：提取上述电脑数量⼤于等于200的字典数据
dict1 = {key: value for (key, value) in counts.items() if value > 200}
print(dict1)
print()
print('-----------集合推导式----------------------')
# 需求：创建⼀个集合，数据为下⽅列表的2次⽅。
list1 = [1, 1, 2]
set1 = {2 ** i for i in list1}
print(set1)

